package com.cgnpc.scp.purchase.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.scp.common.pojo.ActivityInfoParams;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.purchase.service.ICloseProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/******************************
 * 用途说明: 调试采购-需求关闭审批流程pur_demClose
 * 作者姓名: chenshijie
 * 创建时间: 2020/09/19 19:06
 ******************************/
@RestController
@RequestMapping("/scp/purchase/closeProcess")
public class CloseProcessController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContractProcessController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private ICloseProcessService closeProcessService;

    /**********************************
     * 用途说明: 保存草稿/启动流程  因为（保存草稿、启动流程）与（提交流程，回退流程）是两类不同的场景下的操作，故拆分成两个不同的方法
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @PostMapping("/insertContract")
//    @RequiresPermissions("win_add")
    @UBA(module = "调试采购",action = "需求关闭审批流程保存草稿/创建流程",channel = OperatorType.Button)
    public AjaxResult insertContract(HttpServletRequest request, @RequestBody String params) {
        try {
            ApiResult apiResult = closeProcessService.insertContract(params);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,apiResult);
        } catch (Exception e) {
            LOGGER.error("ContractProcessController insertContract error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 获取环节业务数据
     * 参数说明 id
     * 返回值说明:
     ***********************************/
    @PostMapping("/getActivityBusinessInfo")
//    @RequiresPermissions("test")
    @UBA(module = "调试采购",action = "需求关闭审批流程各个环节业务数据",channel = OperatorType.Button)
    public AjaxResult getContractDetails(HttpServletRequest request,@RequestBody ActivityInfoParams activityInfoParams) {
        try {
            Object obj = closeProcessService.getActivityBusinessInfo(activityInfoParams);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,obj);
        } catch (Exception e) {
            LOGGER.error("ContractProcessController getContractDetails error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 删除业务数据以及我的草稿
     * 参数说明 paramsMap
     * 返回值说明:
     ***********************************/
    @PostMapping("/deleteApplyAndMyDraft")
    @UBA(module = "调试采购",action = "需求关闭审批流程删除草稿",channel = OperatorType.Button)
    public AjaxResult deleteWinAndMyDraft(HttpServletRequest request,@RequestBody Map paramsMap){
        try{
            ApiResult apiResult = closeProcessService.deleteApplyAndMyDraft(paramsMap);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,apiResult);
        }catch (Exception e){
            LOGGER.error("ContractProcessController deleteWinAndMyDraft error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 提交/回退流程 因为（保存草稿、启动流程）与（提交流程，回退流程）是两类不同的场景下的操作，故拆分成两个不同的方法
     * 参数说明 params
     * 返回值说明:
     ***********************************/
    @PostMapping("/submitProcess")
//    @RequiresPermissions("win_add")
    @UBA(module = "调试采购",action = "需求关闭审批流程审批",channel = OperatorType.Button)
    public AjaxResult submitProcess(HttpServletRequest request,@RequestBody String params) {
        try {
            ApiResult apiResult = closeProcessService.submitProcess(params);
            return AjaxResultUtil.createI18nDefaultSuccess(request,messageSource,apiResult);
        } catch (Exception e) {
            LOGGER.error("ContractProcessController insertContract error : {}",e.getMessage(),e);
            return AjaxResultUtil.createI18nDefaultError(request,messageSource,e.getMessage());
        }
    }

}
